package com.xm.xmapp.customview;

import java.util.Timer;
import java.util.TimerTask;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.util.DisplayMetrics;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.xm.xmapp.R;
import com.xm.xmapp.gridpasswordview.GridPasswordView;
import com.xm.xmapp.gridpasswordview.GridPasswordView.OnPasswordChangedListener;
import com.xm.xmapp.myinterface.DialogInterFace;

public class PasswordDialog extends Dialog {

	private Context context;
	private String title_str;
	private String content_str;

	private TextView left;
	private String left_str;

	private LinearLayout lin;

	private DialogInterFace dialogInterFace;
	private GridPasswordView gridPasswordView;
	/**
	 * 0是免费预约次数用完1是取消预约2立即用车
	 */
	private int flag;
	public PasswordDialog(Context context, String title, String content, int flag) {
		super(context, R.style.Dialog);
		this.context = context;
		this.title_str = title;
		this.content_str = content;
		this.flag = flag;
	}

	public void init() {
		LayoutInflater inflater = LayoutInflater.from(context);
		View view = inflater.inflate(R.layout.dialog_password, null);
		TextView title = (TextView) view.findViewById(R.id.title);
		TextView content = (TextView) view.findViewById(R.id.content);
		left = (TextView) view.findViewById(R.id.left);

		gridPasswordView = (GridPasswordView) view.findViewById(R.id.pswView);
		lin = (LinearLayout) view.findViewById(R.id.lin);
		if (flag == 2) {
			gridPasswordView.setVisibility(View.VISIBLE);
			lin.setVisibility(View.VISIBLE);
			gridPasswordView.setOnPasswordChangedListener(new OnPasswordChangedListener() {

				@Override
				public void onTextChanged(String psw) {

				}

				@Override
				public void onInputFinish(String psw) {
					if (dialogInterFace != null)
						dialogInterFace.right(flag, psw);
				}
			});

		} else {
			lin.setVisibility(View.VISIBLE);
			gridPasswordView.setVisibility(View.GONE);
		}

		title.setText(title_str == null ? "" : title_str);
		content.setText(content_str == null ? "" : content_str);
		left.setText(left_str == null ? "" : left_str);
		left.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View arg0) {
				if (dialogInterFace != null)
					dialogInterFace.left(flag, "");
			}
		});

		setContentView(view);

		Window dialogWindow = getWindow();
		WindowManager.LayoutParams lp = dialogWindow.getAttributes();
		DisplayMetrics d = context.getResources().getDisplayMetrics(); // 获取屏幕宽、高用
		lp.width = (int) (d.widthPixels * 0.8); // 高度设置为屏幕的0.6
		dialogWindow.setAttributes(lp);
	}

	@Override
	public void show() {
		super.show();
		
		new Handler().postDelayed(new Runnable() {

			@Override
			public void run() {
				gridPasswordView.forceInputViewGetFocus();
			}
		}, 200);
		
//		Timer timer = new Timer();
//		timer.schedule(new TimerTask() {
//
//			public void run() {
//				gridPasswordView.forceInputViewGetFocus();
//			}
//
//		}, 200);

	}

	public PasswordDialog setDialogInterFace(String left, DialogInterFace dialogInterFace) {
		this.dialogInterFace = dialogInterFace;
		this.left_str = left;
		return this;

	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		init();
	}
}
